<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/3
 * Time: 13:13
 */

//用户查看自己月报列表功能

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$page = isset($_GET['page'])? $_GET['page'] : 1;
$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 1;
$username = isset($_GET['username']) ? $_GET['username'] : '';

if(!is_numeric($page) || !is_numeric($pageSize) || empty($username)) {
    return Response::show(401,'数据不合法');
}

try{
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$offset = ($page-1)*$pageSize;
$sql = "SELECT report_id,report_title,report_time,check_status FROM report WHERE report_maker=? limit ".$offset.",".$pageSize;

$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('s',$username);

$data = array();

if($mysqli_stmt->execute()) {
    $mysqli_stmt->bind_result($id,$title,$time,$status);
    while($mysqli_stmt->fetch()) {
        $list = [];
        $list['id'] = $id;
        $list['title'] = $title;
        $list['time'] = $time;
        $list['status'] = $status;
        $data[] = $list;
    }
    //如果结果集长度为0，说明sql语句是正确的，但是没有查找到数据
    if(count($data)>0) {
        $mysqli_stmt->free_result();
        $mysqli_stmt->close();
        $sql = "SELECT COUNT(*) FROM report WHERE report_maker='$username'";
//        echo $sql;
        $res = $connect->query($sql);
//        echo $connect->error();
        $num = $res->fetch_row();
//        var_dump($res);
        return Response::show(200,$num[0],$data);
    } else {
        return Response::show(402,'此用户没有上传过月报');
    }
} else {
    return Response::show(400,'用户月报列表获取失败');
}